package com.imstlife.vending.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.VideoView;

import com.imstlife.vending.R;

/**
 * Created by lihaifeng on 16/11/1.
 */
public class BackgroundVideo {
    /**
     * 加载背景视频
     */
    public static  void initBackGroundVideo(final Context context, WindowManager windowManager, final VideoView bgVideoView) {
        String uri = "android.resource://" + context.getPackageName() + "/" + R.raw.bg;

        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;
        Log.v("width", width + "");
        Log.v("height", height + "");
        ViewGroup.LayoutParams lp = bgVideoView.getLayoutParams();
        lp.width = width+48;
        lp.height = height+48;
        bgVideoView.setLayoutParams(lp);
        bgVideoView.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.bg));
        bgVideoView.start();
        bgVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });

        bgVideoView
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        bgVideoView.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.bg));
                        bgVideoView.start();

                    }
                });
    }
}
